package worst.shopping.main;

import worst.shopping.R;
import worst.shopping.lists.CreateListActivity;
import worst.shopping.lists.ListViewActivity;
import worst.shopping.products.ManageProductsActivity;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.*;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.*;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.*;
import database.*;

public class MainActivity extends Activity {

	private GridView _gridView;
	private Cursor _cursor;
	private ListDAO _listDAO;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
			
		_listDAO = new ListDAO(getApplication());
		setContentView(R.layout.newmainlayout);
		_cursor = _listDAO.getAllListNamesCursor();
		startManagingCursor(_cursor);
		
		setUpGridView();
	}
	
	
	private void setUpGridView() {
		_gridView = (GridView)this.findViewById(R.id.mainactivity_gridview);
		_gridView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				
				TextView textView = (TextView)view.findViewById(R.id.grid_item_text);
				
				Intent intent = new Intent(MainActivity.this, ListViewActivity.class);
				intent.putExtra(DataBaseKeys.LIST_NAME, textView.getText().toString());
				MainActivity.this.startActivity(intent);
			}
		});
			
		BaseAdapter adapter = new SimpleCursorAdapter(this, R.layout.griditem, _cursor, new String[] { DataBaseKeys.LIST_NAME }, new int[]{ R.id.grid_item_text});
		_gridView.setEmptyView(findViewById(R.id.emptyview));
		_gridView.setAdapter(adapter);		

		//register context menu
		registerForContextMenu(_gridView);	
		
		
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		
		super.onCreateContextMenu(menu, v, menuInfo);

		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.mainactivity_contextmenu, menu);
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
	  AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
	  
	  _cursor.moveToPosition(info.position);
	  String listname = _cursor.getString(_cursor.getColumnIndex(DataBaseKeys.LIST_NAME));
	  
	  switch (item.getItemId()) {
	  	case R.id.rename_list:
	  		renameList(listname);
	  		break;
	  	case R.id.delete_list:
	  		_listDAO.deleteList(listname);
	  		
	  		break;
	  	default:
	  		return super.onContextItemSelected(item);
	  }	  
	  _cursor.requery();
	  return true;
	}

	private void renameList(String oldname) {
		
		AlertDialog.Builder alert = new AlertDialog.Builder(this);

		alert.setTitle("Enter a new Name");

		// Set an EditText view to get user input 
		final EditText input = new EditText(this);
		final String oldName = oldname;
		alert.setView(input);
	
		alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int whichButton) {
			String newListName = input.getText().toString();
			_listDAO.renameList(oldName, newListName);
			_cursor.requery();
		  }
		});

		alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
		  public void onClick(DialogInterface dialog, int whichButton) {
		    // Canceled.
		  }
		});

		alert.show();	
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.menu.mainactivity_menu, menu);
		return true;
	}
	

	public void addNewListButtonClicked(View view)
	{
		Intent intent = new Intent(this, CreateListActivity.class);
		startActivity(intent);			
	}
	
	public void manageProductsButtonClicked(View view)
	{
		Intent intent = new Intent(this, ManageProductsActivity.class);
		startActivity(intent);		
	}
}
